﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: DatEmployee.cs
// 作 者：代码自动生成
// 日 期：2011-10-26 11:07:14
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;

namespace ZX.EHR.Library
{
	/// <summary>
    /// 
    /// </summary>
	[Serializable]
    [Table(Name="DatEmployee")]
    [ZX.DBModule.Table("DatEmployee","EmpID")]
    [Obsolete("已过时,建议使用hrEmployee!",false)]
	public class DatEmployee: ZX.DBModule.BaseEntity
	{
        
        #region Business Methods 业务方法
       
		/// <summary>
        /// 员工ID
        /// </summary>
        private static PropertyInfo< Guid > EmpIDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< Guid >("EmpID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid EmpID
        {
            get { return GetProperty(EmpIDProperty); }
            set { SetProperty(EmpIDProperty, value); }
        }
        
		/// <summary>
        /// 姓名
        /// </summary>
        private static PropertyInfo< string > NameProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("Name"));
        [Column]
        public string Name
        {
            get { return GetProperty(NameProperty); }
            set { SetProperty(NameProperty, value); }
        }
		/// <summary>
        /// 员工编号
        /// </summary>
        private static PropertyInfo< string > EmpCodeProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("EmpCode"));
        [Column]
        public string EmpCode
        {
            get { return GetProperty(EmpCodeProperty); }
            set { SetProperty(EmpCodeProperty, value); }
        }
		/// <summary>
        /// 部门ID
        /// </summary>
        private static PropertyInfo< Guid > DeptIDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< Guid >("DeptID"));
        [Column]
        public Guid DeptID
        {
            get { return GetProperty(DeptIDProperty); }
            set { SetProperty(DeptIDProperty, value); }
        }
        /// <summary>
        /// 部门IDs
        /// </summary>
        private static PropertyInfo<string> DeptIdsProperty = RegisterProperty(typeof(DatEmployee), new PropertyInfo<string>("DeptIds"));
        [Column]
        public string DeptIds
        {
            get { return GetProperty(DeptIdsProperty); }
            set { SetProperty(DeptIdsProperty, value); }
        }
        ///// <summary>
        ///// 职位ID
        ///// </summary>
        //private static PropertyInfo< Guid > PositionIDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< Guid >("PositionID"));
        //[Column]
        //public Guid PositionID
        //{
        //    get { return GetProperty(PositionIDProperty); }
        //    set { SetProperty(PositionIDProperty, value); }
        //}
		/// <summary>
        /// 员工类别
        /// </summary>
        private static PropertyInfo< Guid > EmpUserTypeProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo<Guid>("EmpUserType"));
        [Column]
        public Guid EmpUserType
        {
            get { return GetProperty(EmpUserTypeProperty); }
            set { SetProperty(EmpUserTypeProperty, value); }
        }
		/// <summary>
        /// 入职日期
        /// </summary>
        private static PropertyInfo< DateTime > EntryDateProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< DateTime >("EntryDate"));
        [Column]
        public DateTime EntryDate
        {
            get { return GetProperty(EntryDateProperty); }
            set { SetProperty(EntryDateProperty, value); }
        }
        ///// <summary>
        ///// 离职日期
        ///// </summary>
        //private static PropertyInfo< DateTime > LeaveDateProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< DateTime >("LeaveDate"));
        //[Column]
        //public DateTime LeaveDate
        //{
        //    get { return GetProperty(LeaveDateProperty); }
        //    set { SetProperty(LeaveDateProperty, value); }
        //}
        ///// <summary>
        ///// 异动日期[该日期不需要在这里设置]
        ///// </summary>
        //private static PropertyInfo< DateTime > TransferDateProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< DateTime >("TransferDate"));
        //[Column]
        //public DateTime TransferDate
        //{
        //    get { return GetProperty(TransferDateProperty); }
        //    set { SetProperty(TransferDateProperty, value); }
        //}
		/// <summary>
        /// 个人图片[不需要保存到数据库，保存到路径即可]
        /// </summary>
        private static PropertyInfo< byte[] > PhotoProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< byte[] >("Photo"));
        [Column]
        public byte[] Photo
        {
            get { return GetProperty(PhotoProperty); }
            set { SetProperty(PhotoProperty, value); }
        }
		/// <summary>
        /// 手机号码
        /// </summary>
        private static PropertyInfo< string > MobileProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("Mobile"));
        [Column]
        public string Mobile
        {
            get { return GetProperty(MobileProperty); }
            set { SetProperty(MobileProperty, value); }
        }
        ///// <summary>
        ///// 分机号码
        ///// </summary>
        //private static PropertyInfo< string > ExtelePhoneProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("ExtelePhone"));
        //[Column]
        //public string ExtelePhone
        //{
        //    get { return GetProperty(ExtelePhoneProperty); }
        //    set { SetProperty(ExtelePhoneProperty, value); }
        //}
		/// <summary>
        /// 用户状态 O在职 L离职 H休假 T出差
        /// </summary>
        private static PropertyInfo< string > StatusProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("Status"));
        [Column]
        public string Status
        {
            get { return GetProperty(StatusProperty); }
            set { SetProperty(StatusProperty, value); }
        }
		/// <summary>
        /// 用户性别 Male 男 Female 女
        /// </summary>
        private static PropertyInfo< string > SexProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("Sex"));
        [Column]
        public string Sex
        {
            get { return GetProperty(SexProperty); }
            set { SetProperty(SexProperty, value); }
        }
        ///// <summary>
        ///// 年龄
        ///// </summary>
        //private static PropertyInfo< int > AgeProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< int >("Age"));
        //[Column]
        //public int Age
        //{
        //    get { return GetProperty(AgeProperty); }
        //    set { SetProperty(AgeProperty, value); }
        //}
		/// <summary>
        /// 已婚(Y-是,N-否)
        /// </summary>
        private static PropertyInfo< string > MarriageProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("Marriage"));
        [Column]
        public string Marriage
        {
            get { return GetProperty(MarriageProperty); }
            set { SetProperty(MarriageProperty, value); }
        }
		/// <summary>
        /// 出生日期
        /// </summary>
        private static PropertyInfo< DateTime > BirthDateProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< DateTime >("BirthDate"));
        [Column]
        public DateTime BirthDate
        {
            get { return GetProperty(BirthDateProperty); }
            set { SetProperty(BirthDateProperty, value); }
        }
		/// <summary>
        /// 身份证
        /// </summary>
        private static PropertyInfo< string > IDCodeProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("IDCode"));
        [Column]
        public string IDCode
        {
            get { return GetProperty(IDCodeProperty); }
            set { SetProperty(IDCodeProperty, value); }
        }
		/// <summary>
        /// 学历
        /// </summary>
        private static PropertyInfo< string > EducGRDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("EducGRD"));
        [Column]
        public string EducGRD
        {
            get { return GetProperty(EducGRDProperty); }
            set { SetProperty(EducGRDProperty, value); }
        }
		/// <summary>
        /// 英文名
        /// </summary>
        private static PropertyInfo< string > EnNameProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< string >("EnName"));
        [Column]
        public string EnName
        {
            get { return GetProperty(EnNameProperty); }
            set { SetProperty(EnNameProperty, value); }
        }
		/// <summary>
        /// 职级ID
        /// </summary>
        private static PropertyInfo< Guid > RankIDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< Guid >("RankID"));
        [Column]
        public Guid RankID
        {
            get { return GetProperty(RankIDProperty); }
            set { SetProperty(RankIDProperty, value); }
        }
        ///// <summary>
        ///// 职称
        ///// </summary>
        //private static PropertyInfo< Guid > TitleIDProperty = RegisterProperty(typeof(DatEmployee),new PropertyInfo< Guid >("TitleID"));
        //[Column]
        //public Guid TitleID
        //{
        //    get { return GetProperty(TitleIDProperty); }
        //    set { SetProperty(TitleIDProperty, value); }
        //} 
		/// <summary>
        /// 岗位
        /// </summary>
        private static PropertyInfo<Guid> PostIDProperty = RegisterProperty(typeof(DatEmployee), new PropertyInfo<Guid>("PostID"));
        [Column]
        public Guid PostID
        {
            get { return GetProperty(PostIDProperty); }
            set { SetProperty(PostIDProperty, value); }
        }
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "Name");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "EmpCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EmpCode", 30));
            //ValidationRules.AddRule(CommonRules.StringRequired, "EmpUserType");
            //ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EmpUserType", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "Mobile");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Mobile", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "ExtelePhone");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ExtelePhone", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "Status");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Status", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "Sex");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Sex", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "Marriage");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Marriage", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "IDCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("IDCode", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "EducGRD");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EducGRD", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "EnName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EnName", 50));
            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public DatEmployee(){
            this.EmpID = Guid.NewGuid();
        }

        public static DatEmployee New()
        {
            return DataPortal.Create< DatEmployee>();
        }
        public static DatEmployee Get(Guid EmpID)
        {
            return DataPortal.Fetch< DatEmployee>(new SingleLazyCriteria< DatEmployee, Guid>(EmpID, false));
        }
        public static void Delete(Guid EmpID)
        {
            DataPortal.Delete(EmpID);
        }

        //public override DatEmployee Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid EmpID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("EmpID").Equal(EmpID);
                var data = db.Select< DatEmployee>(q)[0];
                LoadProperty(EmpIDProperty, data.EmpID);
                LoadProperty(NameProperty, data.Name);
                LoadProperty(EmpCodeProperty, data.EmpCode);
                LoadProperty(DeptIDProperty, data.DeptID);
                LoadProperty(DeptIdsProperty, data.DeptIds);
                //LoadProperty(PositionIDProperty, data.PositionID);
                LoadProperty(EmpUserTypeProperty, data.EmpUserType);
                LoadProperty(EntryDateProperty, data.EntryDate);
                //LoadProperty(LeaveDateProperty, data.LeaveDate);
                //LoadProperty(TransferDateProperty, data.TransferDate);
                LoadProperty(PhotoProperty, data.Photo);
                LoadProperty(MobileProperty, data.Mobile);
                //LoadProperty(ExtelePhoneProperty, data.ExtelePhone);
                LoadProperty(StatusProperty, data.Status);
                LoadProperty(SexProperty, data.Sex);
                //LoadProperty(AgeProperty, data.Age);
                LoadProperty(MarriageProperty, data.Marriage);
                LoadProperty(BirthDateProperty, data.BirthDate);
                LoadProperty(IDCodeProperty, data.IDCode);
                LoadProperty(EducGRDProperty, data.EducGRD);
                LoadProperty(EnNameProperty, data.EnName);
                LoadProperty(RankIDProperty, data.RankID);
                //LoadProperty(TitleIDProperty, data.TitleID);
                LoadProperty(PostIDProperty, data.PostID);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(EmpIDProperty);
        }
        #endregion

        public override string CheckData()
        {
            if (this.Name == "")
            {
                return "姓名不能为空!";
            }
            else if (this.DeptID == new Guid())
            {
                return "主职部门不能为空!";
            }
            else if (this.DeptIds.Length > 80000)
            {
                return "兼职部门太多，字符太长保存失败!";
            }
            else if (this.PostID == new Guid())
            {
                return "所属职位不能为空!";
            }
            return base.CheckData();
        }

	}
    [Serializable]
    [Table(Name = "EmpGoods")]
    [ZX.DBModule.Table("EmpGoods", "Goods")]
    public class EmpGoods : ZX.DBModule.BaseEntity
    {
       [Column, PK]
        public Guid GoodsID
        {
            get;
            set;
        }

       [Column]
       public Guid EmpID
       {
           get;
           set;
       }

        [Column]
       public string GoodsName
       { get; set; }

        [Column]
        public int Total
        {
            get;
            set;
        }

        [Column]
       public Guid AddUser
       {
           get;
           set;
       }

        [Column]
       public DateTime AddTime
       {
           get;
           set;
       }
    }
}

